import { _decorator, Animation, Component, Node } from 'cc';
import { Emitter } from '../../FrameWork/Common/Emitter';
import { CtrlBase } from './CtrlBase';
const { ccclass, property } = _decorator;

@ccclass('StateCtrl')
export class StateCtrl extends CtrlBase {
    //当前角色的状态
    private _state: string = "None";
    //角色的动画组件
    private _animation: Animation = null;

    //初始状态
    onInit() {
        //获取动画组件
        this._animation = this._owner.getComponent(Animation);
        //注册消息机制
        this._registEmitterEvent();
    }

    //注册消息
    _registEmitterEvent() {
        this.on("Move", () => {
            this.changeState("ZomMove");
        }, this);
        this.on("Attack", () => {
            this.changeState("ZomAttack");
        }, this)
    }


    //状态机，控制状态的切换
    changeState(state: string) {
        if (state == this._state) {
            return;
        }
        this._state = state;
        //停止当前真正播放的动画
        this._animation.stop();
        //播放指定动画
        this._animation.play(this._state);
    }
}


